package com.happycat.service.impl;
 
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.Pagination;
import com.happycat.ResponseHeaderModel;
import com.happycat.ResponseModel;
import com.happycat.SysContants;
import com.happycat.mapper.UserOfficialAccountMapper;
import com.happycat.mobile.model.UserOfficialAccount;
import com.happycat.mobile.service.GenerateIdService;
import com.happycat.mobile.service.UserOfficialAccountService;
import com.happycat.utils.ResponseModelUtils;
/**
 * Module: UserOfficialAccount.java
 * 
 * @author liaoyong
 * @since JDK 1.8
 * @version 1.0
 * @date 2020-08-02
 * @Descriptions:
 */
@Service
public class UserOfficialAccountServiceImpl extends ServiceImpl<UserOfficialAccountMapper, UserOfficialAccount> implements UserOfficialAccountService {
 	
	@Autowired
	private GenerateIdService generateIdService;
 	public ResponseModel<UserOfficialAccount> selectOne(UserOfficialAccount userOfficialAccount){
 		Wrapper<UserOfficialAccount> queryWrapper = new QueryWrapper<>(userOfficialAccount);
		ResponseModel<UserOfficialAccount> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(getOne(queryWrapper));
		return responseModel;
 	
 	}
	
	public ResponseHeaderModel	add(UserOfficialAccount userOfficialAccount){
		ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
//		userOfficialAccount.setId(generateIdService.nextDefaultSerialNumber());
//		userOfficialAccount.setCreateTime(new Date());
		boolean result = save(userOfficialAccount);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("新增成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("新增失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseHeaderModel	modify(UserOfficialAccount userOfficialAccount){
			ResponseHeaderModel responseHeaderModel = new ResponseHeaderModel();
		
		boolean result = updateById(userOfficialAccount);
		if(result) {
			responseHeaderModel.setCode(SysContants.SYS_SUCCESS_CODE);
			responseHeaderModel.setMessage("修改成功");
		}else {
			responseHeaderModel.setCode(SysContants.SYS_FAIL_CODE);
			responseHeaderModel.setMessage("修改失败");
		}
		return responseHeaderModel;
	}
	
	public ResponseModel<Page<UserOfficialAccount>> pageList(Pagination<UserOfficialAccount>page){
		ResponseModel<Page<UserOfficialAccount>> responseModel = new ResponseModel<>();
		responseModel.setCode(SysContants.SYS_SUCCESS_CODE);
		responseModel.setMessage("查询成功");
		responseModel.setData(this.baseMapper.pageList(page));
		return responseModel;
	}

	@Override
	public ResponseModel<UserOfficialAccount> getCount(UserOfficialAccount userOfficialAccount) {
		if(StringUtils.isNotBlank(userOfficialAccount.getAccountId()+"") ) {
			QueryWrapper<UserOfficialAccount> queryWrapper = new QueryWrapper<UserOfficialAccount>();
			queryWrapper.eq("account_id", userOfficialAccount.getAccountId());
			int count= count(new QueryWrapper<UserOfficialAccount>(userOfficialAccount));
			userOfficialAccount.setFansQuantity(count);
			QueryWrapper<UserOfficialAccount> queryWrapperF = new QueryWrapper<UserOfficialAccount>();
			queryWrapperF.eq("focus_id", userOfficialAccount.getAccountId());
			int total= count(queryWrapperF);
			userOfficialAccount.setFocusQuantity(total);
			return ResponseModelUtils.success("查询成功", userOfficialAccount);
		}
		return ResponseModelUtils.fail("参数格式不正确", null);
	}
}
